//
//  RemoveAccountView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 17.03.25.
//

import SwiftUI

struct RemoveAccountView: View {
    
    @EnvironmentObject var appState: AppState
    @Environment(\.presentToast) var presentToast

    @StateObject var viewModel = RemoveAccountViewModel()
    @State var isChecked:Bool = false
    @State var removeConfirm:Bool = false
    var body: some View {
        VStack {
            KoiWebView(url: "http://47.99.180.199/about/remove_account.html")
            
            VStack {
                Toggle(isOn: $isChecked) {
                    Text("我已阅读并同意")
                        .font(.system(size: 13))
                        .foregroundColor(Color.koiColorTextMain)
                    Text("《用户注销协议》")
                        .font(.system(size: 13))
                        .foregroundColor(Color.koiColorPrivacyConfirmHighlightText)
                }.toggleStyle(CheckBoxStyle())
                Button(action: {
                    if isChecked {
                        removeConfirm = true
                    } else {
                        presentToast(.init(message: "请阅读并同意协议"))
                    }
                }) {
                    Text("下一步")
                        .font(.system(size: 16))
                        .fontWeight(.medium)
                        .foregroundColor(Color.white)
                        .padding(EdgeInsets(top: 16, leading: 0, bottom: 16, trailing: 0))
                        .frame(width: 272)
                        .background(Color.koiColorPrimary)
                        .cornerRadius(40)
                        .padding(.bottom, 20)
                }
            }
        }
        .onChange(of: viewModel.state) { value in
            if value == "success" {
                appState.isLoggedIn = false
            }
        }
        .actionSheet(isPresented: $removeConfirm) {
            ActionSheet(
                title: Text("‼️重要提醒‼️"),
                message: Text("为了防止误操作，请您再次确认是否要注销帐号，注销帐号操作不可恢复，建议你在最终确认前备份好帐号相关数据，注销后帐号将无法使用，也无法找回您在时之清单中的任何内容和信息"),
                buttons: [
                    .destructive(Text("确认注销"), action: {
                        viewModel.removeAccount()
                    }),
                    .cancel(Text("取消"))
                ])
        }
    }
}

#Preview {
    RemoveAccountView()
}
